package com.smart.campus.web.controller.school;

import java.util.List;

import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.system.service.ISysDeptService;
import com.smart.campus.web.utils.SchoolUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.school.domain.Grade;
import com.smart.campus.school.service.IGradeService;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.common.core.page.TableDataInfo;

/**
 * 年级Controller
 * 
 * @author Zhihe.Li
 * @date 2020-05-18
 */
@Controller("grade")
@RequestMapping("/school/grade")
public class GradeController extends BaseController
{
    private String prefix = "school/grade";

    @Autowired
    private IGradeService gradeService;
    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("school:grade:view")
    @GetMapping()
    public String grade()
    {
        return prefix + "/grade";
    }

    /**
     * 查询年级列表
     */
    @RequiresPermissions("school:grade:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Grade grade)
    {
        startPage();
        grade.setShcoolId(SchoolUtil.getSchoolId());
        List<Grade> list = gradeService.selectGradeList(grade);
        return getDataTable(list);
    }

    /**
     * 导出年级列表
     */
    @RequiresPermissions("school:grade:export")
    @Log(title = "年级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Grade grade)
    {
        grade.setShcoolId(SchoolUtil.getSchoolId());
        List<Grade> list = gradeService.selectGradeList(grade);
        ExcelUtil<Grade> util = new ExcelUtil<Grade>(Grade.class);
        return util.exportExcel(list, "grade");
    }

    /**
     * 新增年级
     */
    @GetMapping("/add")
    public String add()
    {

        return prefix + "/add";
    }

    /**
     * 新增保存年级
     */
    @RequiresPermissions("school:grade:add")
    @Log(title = "年级", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Grade grade)
    {
        grade.setShcoolId(SchoolUtil.getSchoolId());
        return toAjax(gradeService.insertGrade(grade));
    }

    /**
     * 修改年级
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Grade grade = gradeService.selectGradeById(id);
        mmap.put("grade", grade);
        return prefix + "/edit";
    }

    /**
     * 修改保存年级
     */
    @RequiresPermissions("school:grade:edit")
    @Log(title = "年级", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Grade grade)
    {
        return toAjax(gradeService.updateGrade(grade));
    }

    /**
     * 删除年级
     */
    @RequiresPermissions("school:grade:remove")
    @Log(title = "年级", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gradeService.deleteGradeByIds(ids));
    }

    /**
     * 获取年级
     */
    @PostMapping("/getGradeList")
    @ResponseBody
    public List<Grade> getGradeList() {

        Grade grade = new Grade();
        grade.setStatus(1);
        grade.setShcoolId(SchoolUtil.getSchoolId());
        return gradeService.selectGradeList(grade);
    }

}
